package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.BspsMarketingAdsMapper;
import cc.rengu.igas.bsps.common.entity.MarketingAds;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 营销广告表操作.
 */
public class BspsMarketingAdsMapperImpl implements BspsMarketingAdsMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);


    /**
     * 查询内管营销广告信息
     *
     * @param instId 机构号
     * @param adType 广告类型
     * @return 广告信息
     * @throws Exception 异常
     */
    @Override
    public List<MarketingAds> selectBspsMarketingAdsListByInstIdAndAdType(String instId, String adType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMarketingAdsListByInstIdAndAdType");
        MarketingAds marketingAds = new MarketingAds();
        marketingAds.setInstId(instId);
        marketingAds.setAdType(adType);
        marketingAds.setBeginTime(DateUtil.getCurrentDate());
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, marketingAds, MarketingAds.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("查询内管营销广告信息为空,instId:<{}>", instId);
            return null;
        }
        return objectList.stream().map(item -> (MarketingAds) item).collect(Collectors.toList());
    }
}
